package lib_model.Module;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;

import lib_dal.Connection.ConnectMSSQL;
import lib_dal.Interface.ISql;
import lib_model.Orther.ReturnObj;

public class Module_acl_account implements ISql {

	public int id;
	public int username_ref;
	public int module_acl_id_ref;
	public String reason;
	public Date date_create;

	public Module_acl_account() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public ReturnObj Select() {
		String sql = "select id, username_ref, module_acl_id_ref, reason, date_create from dbo.module_acl_account";
		ReturnObj obj = new ReturnObj();
		ArrayList<Module_acl_account> arr = new ArrayList<Module_acl_account>();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			ResultSet rs = preState.executeQuery();
			while (rs.next()) {
				Module_acl_account a = new Module_acl_account();
				a.id = rs.getInt("id");
				a.username_ref = rs.getInt("username_ref");
				a.module_acl_id_ref = rs.getInt("module_acl_id_ref");
				a.reason = rs.getString("reason");
				a.date_create = rs.getDate("date_create");
				arr.add(a);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		obj.ObjResult = arr;
		return obj;
	}

	@Override
	public ReturnObj Add() {
		String sql = "insert into dbo.module_acl_account (id,   username_ref,   module_acl_id_ref,   reason,   date_create) values   (?,   ?,   ?,   ?,   GETDATE())";
		ReturnObj obj = new ReturnObj();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setInt(1, this.id);
			preState.setInt(2, this.username_ref);
			preState.setInt(3, this.module_acl_id_ref);
			preState.setString(4, this.reason);
			obj.Result = preState.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

	@Override
	public ReturnObj Update() {
		String sql = "update dbo.module_acl_account set username_ref = ?,   module_acl_id_ref = ?,   reason = ?,   date_create = ? where  id = ?";
		ReturnObj obj = new ReturnObj();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setInt(1, this.username_ref);
			preState.setInt(2, this.module_acl_id_ref);
			preState.setString(3, this.reason);
			preState.setDate(4, (java.sql.Date) this.date_create);
			preState.setInt(5, this.id);
			obj.Result = (preState.executeUpdate() != 0) ? true : false;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

	@Override
	public ReturnObj Delete() {
		String sql = "delete from dbo.module_acl_account where id = ?";
		ReturnObj obj = new ReturnObj();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setInt(1, this.id);
			obj.Result = preState.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

	@Override
	public ReturnObj GetInformation() {
		ReturnObj obj = new ReturnObj();
		String sql = "select id, username_ref, module_acl_id_ref, reason, date_create from dbo.module_acl_account where id = ?";
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setInt(1, this.id);
			ResultSet rs = preState.executeQuery();
			while (rs.next()) {
				Module_acl_account a = new Module_acl_account();
				a.id = rs.getInt("id");
				a.username_ref = rs.getInt("username_ref");
				a.module_acl_id_ref = rs.getInt("module_acl_id_ref");
				a.reason = rs.getString("reason");
				a.date_create = rs.getDate("date_create");
				obj.ObjResult = a;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

}
